package com.gepengjun.lims.util.xssf;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.List;

public class XSSFUtil {


    private XSSFWorkbook xssfWorkbook;

    private XSSFSheet xssfSheet;

    public XSSFUtil() {
    }

    public XSSFUtil(XSSFWorkbook xssfWorkbook) {
        this.xssfWorkbook = xssfWorkbook;
    }

    public XSSFSheet createSheet(String sheetName,int i){
        XSSFSheet sheet = xssfWorkbook.createSheet(sheetName);
        if (i > 0){
            for (int j = 0;j < i; j++){
                sheet.setColumnWidth(j,25*256);
            }
        }
        return sheet;
    }


    public Row createRow(Sheet sheet,int i){
        Row row = sheet.createRow(i);
        return row;
    }

    public Row createRow(Sheet sheet,int i,Short height){
        Row row = sheet.createRow(i);
        row.setHeight(height);
        return row;
    }



    public Cell createCell(Row row,int i,CellType dataType){

        Cell cell = row.createCell(i,dataType);
        CellStyle cellStyle = this.xssfWorkbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setWrapText(true);
        cellStyle.setDataFormat((short) dataType.ordinal());
        cell.setCellStyle(cellStyle);
        return cell;
    }

    public Cell createTitleCell(Row row,int i){
        Cell cell = row.createCell(i);
        CellStyle cellStyle = this.xssfWorkbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setWrapText(true);
        XSSFFont font = xssfWorkbook.createFont();
        font.setBold(true);
        font.setFontHeight(17d);
        cell.setCellStyle(cellStyle);
        return cell;
    }

    public Cell createNumberCell(Row row,int i){
        Cell cell = row.createCell(i);
        CellStyle cellStyle = this.xssfWorkbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setWrapText(true);
        cellStyle.setDataFormat((short)2);
        return cell;
    }


    public Object getCellValue(Cell cell){
        if (cell == null){
            return null;
        }
        Object o = null;
        CellType cellType = cell.getCellType();
        switch (cellType){
            case STRING:
                o = cell.getStringCellValue();
                break;
            case BOOLEAN:
                o = cell.getBooleanCellValue();
                break;
            case NUMERIC:
                o = getNumericCellValue(cell);
                break;
            case BLANK:
                o = "";
                break;
            case FORMULA:
                try {
                    o = cell.getNumericCellValue();
                }catch (IllegalStateException e){
                    try {
                        o = cell.getRichStringCellValue().toString();
                    }catch (IllegalStateException e1){
                        o = cell.getErrorCellValue();
                    }

                }
                break;
            default:
                o = cell.getRichStringCellValue().toString();

        }
        //if (cell.getCellType().name().equals())



        return o;
    }

    private Object getNumericCellValue(Cell cell){
        Object o = null;
        boolean isDate = DateUtil.isCellDateFormatted(cell);
        if (isDate){
            o = cell.getDateCellValue();
        }else {
            o = cell.getNumericCellValue();
        }
        return o;
    }
}
